package com.sky.controller.user;

import com.alibaba.druid.sql.visitor.functions.Isnull;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "C端-用户下单相关接口")
@RestController("userOrdersController")
@Slf4j
@RequestMapping("/user/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * C端-用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @ApiOperation("C端-用户下单")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单：{}",ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = ordersService.submit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * C端-历史订单查询
     * @param pageSize
     * @param page
     * @param status
     * @return
     */
    @ApiOperation("C端-历史订单查询")
    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrders(@RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "") Integer status){
        log.info("C端-历史订单查询：每页记录数->{},页面->{},订单状态->{}", pageSize,page,status);
        PageResult pageResult = ordersService.historyOrders(pageSize,page,status);
        return Result.success(pageResult);
    }


    /**
     * C端-查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("C端-根据订单id查询订单详情")
    public Result<OrderVO> selectOrderDetail(@PathVariable Long id){
        log.info("C端-根据订单id查询订单详情：{}", id);
        OrderVO orderVO = ordersService.selectOrderDetail(id);
        return Result.success(orderVO);
    }


    @PutMapping("/cancel/{id}")
    @ApiOperation("C端-取消订单")
    public Result cancelOrder(@PathVariable Long id){
        log.info("C端-根据订单id取消订单:{}", id);
        ordersService.cancelOrderById(id);
        return Result.success("订单取消成功！");
    }

    /**
     * C端-根据订单id再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("C端-再来一单")
    public Result repetitionByOrderId(@PathVariable Long id){
        log.info("C端-根据订单id再来一单:{}", id);
        //再来一单就是将原订单中的商品重新加入到购物车中
        ordersService.repetitionByOrderId(id);
        return Result.success("再来一单成功！");
    }

    /**
     * 用户催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("用户催单")
    public Result reminder(@PathVariable("id") Long id) {
        ordersService.reminder(id);
        return Result.success();
    }



}
